<?php
namespace App\DBs;

use Illuminate\Support\Facades\DB;

class UserDB extends BaseDB
{
    /**
     * 评论
     */

    public $tUser = 'user';

    public function __construct()
    {
    }

    /**
     * 一个
     */
    public function getOneByWx($wx)
    {
        $models = DB::table($this->tUser)->where('del',0)->where('wx',$wx)->get();
        if (!count($models)) { return array('code'=>'-1','msg'=>'没有用户'); }
        $model = $this->jsonToArr($models[0]);
        $isnames = ['没有','申请中','拒绝','已通过'];
        $isGuoName = array_key_exists($model['isguo'],$isnames) ? $isnames[$model['isguo']] : '';
        $isShopName = array_key_exists($model['isshop'],$isnames) ? $isnames[$model['isshop']] : '';
        $items = [
            'uname'=>$model['uname'], 'phone'=>$model['phone'], 'wx'=>$model['wx'],
            'head'=>$model['head'], 'isguo'=>$model['isguo'], 'isshop'=>$model['isshop'],
            'isGuoName'=>$isGuoName, 'isShopName'=>$isShopName,
        ];
        return array('code'=>'0','msg'=>'success','data'=>$items);
    }

    /**
     * 设置昵称
     */
    public function setUname($wx,$uname)
    {
        //开启事务
        DB::beginTransaction();
        try {
            $resDb = DB::table($this->tUser)->where('wx',$wx)->update(['uname'=>$uname]);
            if (!$resDb) { return array('code'=>'-2','msg'=>'操作失败'); }
            //提交事务
            DB::commit();
            return array('code'=>'0', 'msg'=>'操作成功');
        } catch (\Exception $e) {
            //事务回滚
            DB::rollBack();
            return array('code'=>'-1', 'msg'=>'操作失败：'.$e);
        }
    }

    /**
     * 申请果农
     */
    public function setGuoApply($wx)
    {
        //开启事务
        DB::beginTransaction();
        try {
            $resDb = DB::table($this->tUser)->where('wx',$wx)->update(['isguo'=>1]);
            if (!$resDb) { return array('code'=>'-2','msg'=>'操作失败'); }
            //提交事务
            DB::commit();
            return array('code'=>'0', 'msg'=>'操作成功');
        } catch (\Exception $e) {
            //事务回滚
            DB::rollBack();
            return array('code'=>'-1', 'msg'=>'操作失败：'.$e);
        }
    }

    /**
     * 申请商家
     */
    public function setShopApply($wx)
    {
        //开启事务
        DB::beginTransaction();
        try {
            $resDb = DB::table($this->tUser)->where('wx',$wx)->update(['isshop'=>1]);
            if (!$resDb) { return array('code'=>'-2','msg'=>'操作失败'); }
            //提交事务
            DB::commit();
            return array('code'=>'0', 'msg'=>'操作成功');
        } catch (\Exception $e) {
            //事务回滚
            DB::rollBack();
            return array('code'=>'-1', 'msg'=>'操作失败：'.$e);
        }
    }
}